import streamlit as st
from database import verify_user
import os

# 获取当前路径
curr_path = os.getcwd()
image_path = os.path.join(curr_path, "assets")

# 加载图标
def load_icon(icon_name):
    """加载图标"""
    return os.path.join(image_path, f"{icon_name}.png")

# 设置页面标题和Logo
col1, col2 = st.columns([1, 7])
with col1:
    st.image(load_icon("logo"), width=80)  # Logo 大小设置为 46px
with col2:
    st.title("登录机器猫智能客服系统")
st.markdown("---")

# 登录表单
def login_form():
    with st.form("登录"):
        username = st.text_input("用户名")
        password = st.text_input("密码", type="password")
        submit_button = st.form_submit_button("登录")

        if submit_button:
            user = verify_user(username, password)
            if user:
                st.session_state.user = user  # 设置用户信息
                st.success("登录成功！")
                st.switch_page("main.py")  # 跳转到首页
                st.rerun()   # 重新运行页面以触发跳转
            else:
                st.error("用户名或密码错误")

# 显示登录表单
login_form()

# 注册按钮
st.markdown("---")
st.markdown("还没有账号？")
if st.button("注册账号"):
    st.switch_page("pages/2_regedit.py")  # 跳转到注册页面

# 版权信息
st.markdown("---")
st.markdown("©2024-2025 Doraemon-Bot 光环AIE-51-1G保留所有权利。")
